<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Linux Terminal Simulator - HTML4</title>
    <style type="text/css">
        body {
            background-color: #000000;
            background-image: url('');
            color: #00ff00;
            font-family: "Courier New", Courier, monospace;
            font-size: 12px;
            margin: 0;
            padding: 20px;
            overflow: auto;
        }

        .terminal-container {
            width: 95%;
            height: 500px;
            background-color: #000000;
            border: 2px inset #c0c0c0;
            padding: 10px;
            overflow: auto;
        }

        .terminal-header {
            background-color: #c0c0c0;
            color: #000000;
            padding: 5px;
            text-align: center;
            font-weight: bold;
            border: 1px outset #c0c0c0;
            margin-bottom: 5px;
        }

        .terminal-output {
            white-space: pre;
            word-wrap: break-word;
            font-family: "Courier New", Courier, monospace;
            font-size: 12px;
            line-height: 1.2;
        }

        .prompt {
            color: #00ff00;
        }

        .input-line {
            margin-top: 5px;
        }

        .terminal-input {
            background-color: #000000;
            color: #00ff00;
            border: none;
            font-family: "Courier New", Courier, monospace;
            font-size: 12px;
            width: 80%;
            outline: none;
        }

        .error {
            color: #ff0000;
        }

        .success {
            color: #00ff00;
        }

        .warning {
            color: #ffff00;
        }

        .directory {
            color: #0080ff;
            font-weight: bold;
        }

        .executable {
            color: #00ff00;
            font-weight: bold;
        }

        .process-table {
            border-collapse: collapse;
            width: 100%;
            color: #00ff00;
        }

        .process-table td, .process-table th {
            border: 1px solid #008000;
            padding: 3px;
            text-align: left;
        }

        .process-table th {
            background-color: #004000;
        }

        .cursor {
            background-color: #00ff00;
            color: #000000;
        }
    </style>
</head>
<body>
    <div class="terminal-container">
        <div class="terminal-header">Linux Terminal Simulator - HTML4</div>
        <div class="terminal-output" id="output">Welcome to Linux Terminal Simulator (HTML4)
Type 'help' for available commands

</div>
        <div class="input-line">
            <span class="prompt" id="prompt">user@linux:~$</span>
            <input type="text" class="terminal-input" id="input" autocomplete="off">
            <span class="cursor">_</span>
        </div>
    </div>

    <script type="text/javascript">
        function LinuxSimulator() {
            this.currentPath = '/home/user';
            this.fileSystem = {
                '/': {
                    type: 'directory',
                    children: {
                        'home': {
                            type: 'directory',
                            children: {
                                'user': {
                                    type: 'directory',
                                    children: {
                                        'documents': { type: 'directory', children: {} },
                                        'downloads': { type: 'directory', children: {} },
                                        'test.txt': { type: 'file', content: 'Hello World!\nThis is a test file.' },
                                        'script.sh': { type: 'file', content: '#!/bin/bash\necho "Hello from script"', executable: true }
                                    }
                                }
                            }
                        },
                        'etc': {
                            type: 'directory',
                            children: {
                                'passwd': { type: 'file', content: 'root:x:0:0:root:/root:/bin/bash\nuser:x:1000:1000:user:/home/user:/bin/bash' },
                                'hosts': { type: 'file', content: '127.0.0.1 localhost\n::1 localhost' }
                            }
                        },
                        'var': { type: 'directory', children: {} },
                        'tmp': { type: 'directory', children: {} }
                    }
                }
            };
            this.processes = [
                { pid: 1, user: 'root', cpu: 0.1, mem: 0.2, command: 'init' },
                { pid: 123, user: 'user', cpu: 2.3, mem: 1.5, command: 'bash' },
                { pid: 456, user: 'user', cpu: 0.0, mem: 0.8, command: 'vim' }
            ];
            this.commandHistory = [];
            this.historyIndex = -1;
            this.setupEventListeners();
        }

        LinuxSimulator.prototype.setupEventListeners = function() {
            var self = this;
            var input = document.getElementById('input');
            
            input.onkeydown = function(e) {
                e = e || window.event;
                if (e.keyCode === 13) { // Enter
                    self.executeCommand(input.value);
                    input.value = '';
                } else if (e.keyCode === 38) { // Arrow Up
                    if (e.preventDefault) e.preventDefault();
                    self.navigateHistory(-1);
                } else if (e.keyCode === 40) { // Arrow Down
                    if (e.preventDefault) e.preventDefault();
                    self.navigateHistory(1);
                }
            };
            
            input.focus();
        };

        LinuxSimulator.prototype.navigateHistory = function(direction) {
            var input = document.getElementById('input');
            if (direction === -1 && this.historyIndex < this.commandHistory.length - 1) {
                this.historyIndex++;
                input.value = this.commandHistory[this.commandHistory.length - 1 - this.historyIndex];
            } else if (direction === 1 && this.historyIndex > 0) {
                this.historyIndex--;
                input.value = this.commandHistory[this.commandHistory.length - 1 - this.historyIndex];
            } else if (direction === 1 && this.historyIndex === 0) {
                this.historyIndex = -1;
                input.value = '';
            }
        };

        LinuxSimulator.prototype.executeCommand = function(command) {
            if (command && command.replace(/\s/g, '')) {
                this.commandHistory.push(command);
                this.historyIndex = -1;
            }

            this.appendOutput('<span class="prompt">' + document.getElementById('prompt').innerHTML + '</span> ' + command + '\n');

            var args = command.replace(/^\s+|\s+$/g, '').split(/\s+/);
            var cmd = args[0];

            switch (cmd) {
                case 'help':
                    this.showHelp();
                    break;
                case 'ls':
                    this.listFiles(args);
                    break;
                case 'pwd':
                    this.printWorkingDirectory();
                    break;
                case 'cd':
                    this.changeDirectory(args[1]);
                    break;
                case 'mkdir':
                    this.makeDirectory(args[1]);
                    break;
                case 'rmdir':
                    this.removeDirectory(args[1]);
                    break;
                case 'rm':
                    this.removeFile(args);
                    break;
                case 'touch':
                    this.createFile(args[1]);
                    break;
                case 'cat':
                    this.catFile(args[1]);
                    break;
                case 'more':
                case 'less':
                    this.viewFile(args[1]);
                    break;
                case 'head':
                    this.headFile(args[1]);
                    break;
                case 'tail':
                    this.tailFile(args[1]);
                    break;
                case 'cp':
                    this.copyFile(args[1], args[2]);
                    break;
                case 'mv':
                    this.moveFile(args[1], args[2]);
                    break;
                case 'ps':
                    this.showProcesses(args);
                    break;
                case 'top':
                case 'htop':
                    this.showTop();
                    break;
                case 'df':
                    this.showDiskUsage();
                    break;
                case 'free':
                    this.showMemoryUsage();
                    break;
                case 'ping':
                    this.ping(args[1]);
                    break;
                case 'ifconfig':
                case 'ip':
                    this.showNetworkInfo();
                    break;
                case 'wget':
                case 'curl':
                    this.downloadFile(args[1]);
                    break;
                case 'chmod':
                    this.changePermissions(args[1], args[2]);
                    break;
                case 'chown':
                    this.changeOwner(args[1], args[2]);
                    break;
                case 'passwd':
                    this.changePassword(args[1]);
                    break;
                case 'tar':
                    this.tarOperation(args);
                    break;
                case 'zip':
                    this.zipFiles(args);
                    break;
                case 'unzip':
                    this.unzipFile(args[1]);
                    break;
                case 'ssh':
                    this.sshConnect(args[1]);
                    break;
                case 'scp':
                    this.scpTransfer(args[1], args[2]);
                    break;
                case 'clear':
                    this.clearScreen();
                    break;
                case 'exit':
                    this.appendOutput('<span class="success">Goodbye!</span>\n');
                    break;
                case 'whoami':
                    this.appendOutput('user\n');
                    break;
                case 'date':
                    this.appendOutput(new Date().toString() + '\n');
                    break;
                case 'uname':
                    this.appendOutput('Linux simulator 2.6.32 i686 GNU/Linux\n');
                    break;
                default:
                    if (command && command.replace(/\s/g, '')) {
                        this.appendOutput('<span class="error">bash: ' + cmd + ': command not found</span>\n');
                    }
            }

            this.updatePrompt();
        };

        LinuxSimulator.prototype.showHelp = function() {
            var helpText = 'Available commands:\n' +
                'File Operations:\n' +
                '  ls [-la]        - List directory contents\n' +
                '  pwd             - Print working directory\n' +
                '  cd [dir]        - Change directory\n' +
                '  mkdir <dir>     - Create directory\n' +
                '  rmdir <dir>     - Remove directory\n' +
                '  rm [-rf] <file> - Remove files/directories\n' +
                '  touch <file>    - Create empty file\n' +
                '  cat <file>      - Display file content\n' +
                '  more/less <file>- View file with pagination\n' +
                '  head <file>     - Show first lines of file\n' +
                '  tail <file>     - Show last lines of file\n' +
                '  cp <src> <dst>  - Copy files\n' +
                '  mv <src> <dst>  - Move/rename files\n\n' +
                'System Information:\n' +
                '  ps [aux]        - Show running processes\n' +
                '  top/htop        - Show system processes\n' +
                '  df              - Show disk usage\n' +
                '  free            - Show memory usage\n' +
                '  uname           - Show system information\n' +
                '  whoami          - Show current user\n' +
                '  date            - Show current date/time\n\n' +
                'Network:\n' +
                '  ping <host>     - Ping a host\n' +
                '  ifconfig/ip     - Show network interfaces\n' +
                '  wget/curl <url> - Download files\n\n' +
                'Other:\n' +
                '  clear           - Clear screen\n' +
                '  exit            - Exit terminal\n' +
                '  help            - Show this help\n\n';
            this.appendOutput(helpText);
        };

        LinuxSimulator.prototype.listFiles = function(args) {
            var showAll = false;
            var longFormat = false;
            
            for (var i = 0; i < args.length; i++) {
                if (args[i] === '-a' || args[i] === '-la') showAll = true;
                if (args[i] === '-l' || args[i] === '-la') longFormat = true;
            }
            
            var currentDir = this.getCurrentDirectory();
            if (!currentDir || currentDir.type !== 'directory') {
                this.appendOutput('<span class="error">ls: cannot access directory</span>\n');
                return;
            }

            var files = [];
            for (var name in currentDir.children) {
                files.push(name);
            }
            
            if (showAll) {
                files = ['.', '..'].concat(files);
            }

            if (longFormat) {
                this.appendOutput('total ' + files.length + '\n');
                for (var i = 0; i < files.length; i++) {
                    var name = files[i];
                    if (name === '.' || name === '..') {
                        this.appendOutput('drwxr-xr-x 2 user user 4096 ' + new Date().toDateString() + ' <span class="directory">' + name + '</span>\n');
                    } else {
                        var item = currentDir.children[name];
                        var permissions = item.type === 'directory' ? 'drwxr-xr-x' : (item.executable ? '-rwxr-xr-x' : '-rw-r--r--');
                        var size = item.type === 'directory' ? '4096' : (item.content ? item.content.length.toString() : '0');
                        var className = item.type === 'directory' ? 'directory' : (item.executable ? 'executable' : '');
                        this.appendOutput(permissions + ' 1 user user ' + size + ' ' + new Date().toDateString() + ' <span class="' + className + '">' + name + '</span>\n');
                    }
                }
            } else {
                for (var i = 0; i < files.length; i++) {
                    var name = files[i];
                    if (name === '.' || name === '..') {
                        this.appendOutput('<span class="directory">' + name + '</span>  ');
                    } else {
                        var item = currentDir.children[name];
                        var className = item.type === 'directory' ? 'directory' : (item.executable ? 'executable' : '');
                        this.appendOutput('<span class="' + className + '">' + name + '</span>  ');
                    }
                }
                this.appendOutput('\n');
            }
        };

        LinuxSimulator.prototype.printWorkingDirectory = function() {
            this.appendOutput(this.currentPath + '\n');
        };

        LinuxSimulator.prototype.changeDirectory = function(path) {
            if (!path || path === '~') {
                this.currentPath = '/home/user';
                return;
            }

            if (path === '..') {
                var parts = this.currentPath.split('/');
                var filteredParts = [];
                for (var i = 0; i < parts.length; i++) {
                    if (parts[i]) filteredParts.push(parts[i]);
                }
                if (filteredParts.length > 0) {
                    filteredParts.pop();
                    this.currentPath = '/' + filteredParts.join('/');
                    if (this.currentPath === '/') this.currentPath = '/';
                }
                return;
            }

            if (path === '/') {
                this.currentPath = '/';
                return;
            }

            var newPath;
            if (path.charAt(0) === '/') {
                newPath = path;
            } else {
                newPath = this.currentPath === '/' ? '/' + path : this.currentPath + '/' + path;
            }

            var targetDir = this.getDirectoryByPath(newPath);
            if (targetDir && targetDir.type === 'directory') {
                this.currentPath = newPath;
            } else {
                this.appendOutput('<span class="error">cd: ' + path + ': No such file or directory</span>\n');
            }
        };

        // Additional methods following similar pattern...
        LinuxSimulator.prototype.makeDirectory = function(name) {
            if (!name) {
                this.appendOutput('<span class="error">mkdir: missing operand</span>\n');
                return;
            }

            var currentDir = this.getCurrentDirectory();
            if (currentDir && currentDir.children) {
                if (currentDir.children[name]) {
                    this.appendOutput('<span class="error">mkdir: cannot create directory \'' + name + '\': File exists</span>\n');
                } else {
                    currentDir.children[name] = { type: 'directory', children: {} };
                    this.appendOutput('<span class="success">Directory \'' + name + '\' created</span>\n');
                }
            }
        };

        LinuxSimulator.prototype.catFile = function(name) {
            if (!name) {
                this.appendOutput('<span class="error">cat: missing operand</span>\n');
                return;
            }

            var currentDir = this.getCurrentDirectory();
            if (currentDir && currentDir.children && currentDir.children[name]) {
                var file = currentDir.children[name];
                if (file.type === 'file') {
                    this.appendOutput((file.content || '') + '\n');
                } else {
                    this.appendOutput('<span class="error">cat: ' + name + ': Is a directory</span>\n');
                }
            } else {
                this.appendOutput('<span class="error">cat: ' + name + ': No such file or directory</span>\n');
            }
        };

        LinuxSimulator.prototype.showProcesses = function(args) {
            this.appendOutput('<table class="process-table">');
            this.appendOutput('<tr><th>PID</th><th>USER</th><th>%CPU</th><th>%MEM</th><th>COMMAND</th></tr>');
            
            for (var i = 0; i < this.processes.length; i++) {
                var proc = this.processes[i];
                this.appendOutput('<tr><td>' + proc.pid + '</td><td>' + proc.user + '</td><td>' + proc.cpu + '</td><td>' + proc.mem + '</td><td>' + proc.command + '</td></tr>');
            }
            
            this.appendOutput('</table>\n');
        };

        LinuxSimulator.prototype.clearScreen = function() {
            document.getElementById('output').innerHTML = '';
        };

        LinuxSimulator.prototype.getCurrentDirectory = function() {
            return this.getDirectoryByPath(this.currentPath);
        };

        LinuxSimulator.prototype.getDirectoryByPath = function(path) {
            var parts = path.split('/');
            var filteredParts = [];
            for (var i = 0; i < parts.length; i++) {
                if (parts[i]) filteredParts.push(parts[i]);
            }
            
            var current = this.fileSystem['/'];

            for (var i = 0; i < filteredParts.length; i++) {
                var part = filteredParts[i];
                if (current && current.children && current.children[part]) {
                    current = current.children[part];
                } else {
                    return null;
                }
            }

            return current;
        };

        LinuxSimulator.prototype.updatePrompt = function() {
            var prompt = document.getElementById('prompt');
            var displayPath = this.currentPath === '/home/user' ? '~' : this.currentPath;
            prompt.innerHTML = 'user@linux:' + displayPath + '$';
        };

        LinuxSimulator.prototype.appendOutput = function(text) {
            var output = document.getElementById('output');
            output.innerHTML += text;
            output.scrollTop = output.scrollHeight;
        };

        // Initialize the simulator
        var simulator = new LinuxSimulator();
    </script>
</body>
</html>